package com.bdzt.phone.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.bdzt.phone.R;
import com.bdzt.phone.bean.PhonePar;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class Step1Fragment extends Fragment {
    private Spinner sp_cpu, sp_ram, sp_rom, sp_src, sp_res;
    private List<String> cpu;
    private List<String> ram;
    private List<String> rom;
    private List<String> src;
    private List<String> res;
    PhonePar phonePar;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_step1, container, false);
        initData();
        initView(view);
        return view;
    }

    private void initData() {
        res = new ArrayList<>();
        ram = new ArrayList<>();
        rom = new ArrayList<>();
        src = new ArrayList<>();
        cpu = new ArrayList<>();


        cpu.add("骁龙821");
        cpu.add("骁龙820");
        cpu.add("Exynos 8890");
        cpu.add("Exynos 7420");
        cpu.add("Helio X25");

        ram.add("3G");
        ram.add("4G");
        ram.add("6G");

        rom.add("32G");
        rom.add("64G");
        rom.add("128G");
        rom.add("256G");

        src.add("4.7");
        src.add("5.0");
        src.add("5.5");
        src.add("6.0");

        res.add("720*1280");
        res.add("1080*1920");
        res.add("2k");


    }

    private void initView(View view) {
        phonePar = new PhonePar();

        sp_cpu = (Spinner) view.findViewById(R.id.sp_cpu);
        sp_ram = (Spinner) view.findViewById(R.id.sp_ram);
        sp_rom = (Spinner) view.findViewById(R.id.sp_rom);
        sp_src = (Spinner) view.findViewById(R.id.sp_src);
        sp_res = (Spinner) view.findViewById(R.id.sp_res);

        view.findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                Setp2Fragment setp2Fragment = new Setp2Fragment();
                Bundle bundle=new Bundle();
                bundle.putSerializable("data",phonePar);
                setp2Fragment.setArguments(bundle);

                transaction.replace(R.id.fl_base_context,setp2Fragment);
                transaction.addToBackStack("step1");
                transaction.commit();
            }
        });

        ArrayAdapter<String> phoneCpu = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, cpu);
        ArrayAdapter<String> phoneRam = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, ram);
        ArrayAdapter<String> phoneRom = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, rom);
        ArrayAdapter<String> phoneSrc = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, src);
        ArrayAdapter<String> phoneRes = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, res);


        phoneCpu.setDropDownViewResource(android.R.layout.simple_list_item_checked);
        phoneRam.setDropDownViewResource(android.R.layout.simple_list_item_checked);
        phoneRom.setDropDownViewResource(android.R.layout.simple_list_item_checked);
        phoneSrc.setDropDownViewResource(android.R.layout.simple_list_item_checked);
        phoneRes.setDropDownViewResource(android.R.layout.simple_list_item_checked);


        sp_cpu.setAdapter(phoneCpu);
        sp_ram.setAdapter(phoneRam);
        sp_rom.setAdapter(phoneRom);
        sp_src.setAdapter(phoneSrc);
        sp_res.setAdapter(phoneRes);

        sp_cpu.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                phonePar.setPhoneCPU(cpu.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        sp_ram.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                phonePar.setPhoneRAM(ram.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        sp_rom.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                phonePar.setPhoneROM(rom.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        sp_src.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                phonePar.setPhoneScr(src.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        sp_res.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                phonePar.setPhoneRes(res.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

}
